<template>
    <div>
    <el-upload class="avatar-uploader" :action="actionUrl" :headers="actionHeader" :show-file-list="false"
        :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-button type="primary"  @click="UpdateAvatar">点击更新头像</el-button>
</div>
</template>

<script>
    import appConfig from '@/config/config.js';

    import {
        getToken,
        getUserInfo,
        setUserInfo,
        clearUserInfo
    } from '@/utils/auth'



    import {
        updateuser
    } from '@/api/user'







    export default {
        name: "UserAvatar",
        data() {
            return {
                actionUrl: appConfig.baseUrl + '/File/Upload/FormImg',
                actionHeader: {
                    Authorization: `Bearer ` + getToken()
                },
                imageUrl: ``,
                userinfo: ""
            };
        },
        methods: {
            handleAvatarSuccess(res, resfileinfo) {
                // console.log(res);
                console.log(resfileinfo);
                // this.imageUrl = URL.createObjectURL(file.raw);
                this.imageUrl = appConfig.baseUrl + `/File/dolowdfile?Id=${res.saveFilesInfo[0].id}`;
                //上传完成更新头像
                console.log(JSON.parse(getUserInfo()));
                this.userinfo = JSON.parse(getUserInfo())
                updateuser({
                    Id: this.userinfo.id,
                    Username: this.userinfo.username,
                    Password: this.userinfo.password,
                    AvatarId: res.saveFilesInfo[0].id,
                    Remarks: this.userinfo.remarks
                }).then(res => {
                    clearUserInfo()
                    setUserInfo(res)
                    console.log(res);
                })

            },
            //上传完成后点击更新头像,这一步是作假，代替了刷新,哈哈哈
            UpdateAvatar() {
                // location.href('http://localhost:8080/system/user/avatar')
                this.$router.go(0)
            },
            //头像上传之前做一些判断
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>
<style>
    .avatar-uploader .el-upload {
        border: 3px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>